<?php

namespace gogrpc;

use Proto\AccountServerClient;

class AccountServer extends BaseServer
{

    protected static $client   = null;
    protected static $metadata = [];

    public function __construct()
    {
        // 日志文件
        parent::$log_file = 'account_grpc.log';

        self::$metadata = [
            "app_id"     => [config('grpc.app_id')],
            "app_secret" => [config('grpc.app_secret')],
        ];

        if (!self::$client) {
            self::$client = new AccountServerClient(config('grpc.host'), [
                'credentials' => \Grpc\ChannelCredentials::createInsecure(),
            ]);
        }
    }

    /**
     * 用户踢出操作
     * @param $uid
     * @param int $ban 封禁用户是ban=1，其他默认即可
     * @return bool
     */
    public function logout($uid, $ban = 0)
    {
        $request = new \Proto\LogoutReq();
        $request->setUid($uid);
        $request->setBan($ban);

        list($response, $status) = self::$client->Logout($request, self::$metadata)->wait();
        if ($status->code == \Grpc\STATUS_OK) {
            return true;
        }
        // 记录日志
        parent::writeLog(__FUNCTION__,$status);
        return false;
    }


    public function getUserAccount($uid)
    {
        $request = new \Proto\GetUserInfoReq();
        $request->setUid($uid);

        list($response, $status) = self::$client->GetUserInfo($request, self::$metadata)->wait();
        print_r($status);
        echo $response->getAccount();
    }

}